(function() {



    $.fn.touchTouch2 = function(options) {

        /*ajax动态加载使用$(dom[class!=<touchTouchEnd>])的方式避免和已经实例化的dom冲突*/
        return $(this).each(function(index) {
            /*默认参数*/
            var defaults = {
                parent: "body",
                target: ".j-pinlunImg",
                overlay: $('<div class="galleryOverlay">'),
                slider: $('<div class="gallerySlider">'),
                prevArrow: $('<a class="prevArrow"></a>'),
                nextArrow: $('<a class="nextArrow"></a>'),
                overlayVisible: false,
                overlayClass: ".galleryOverlay",
                sliderClass: "gallerySlider",
                prevArrowClass: "prevArrow",
                nextArrowClass: "nextArrow"
            }

            /*遍历的时候重新获取*/
            var placeholders = $([]),
                index = 0,
                items = null;
            var opts = $.extend({}, defaults, options);
            var This = $(this),
                parent = $(this),
                target = opts.target,
                items = parent.find(target),
                overlay = opts.overlay,
                slider = opts.slider,
                prevArrow = opts.prevArrow,
                nextArrow = opts.nextArrow,
                overlayVisible = opts.overlayVisible,
                overlayClass = opts.overlayClass,
                sliderClass = opts.sliderClass,
                overlayVisible = opts.overlayVisible,
                touchTouchEnd="touchTouchEnd";/*已经实例化后添加的标识,*/


            // Appending the markup to the page

            overlay.hide().appendTo(parent);
            parent.addClass(touchTouchEnd)


            slider.appendTo(overlay);
            // Creating a placeholder for each image
            items.each(function() {
                placeholders = placeholders.add($('<div class="placeholder">'));
            });


            slider.append(placeholders).on('click', function(e) {

              if (!$(e.target).is('img') && (e.target).className!='ckyw') {
                    hideOverlay();
                  return false;
               }
            });
            /*重新获取节点*/
            var Changehua=true;
            var LONGPRESS=true;//长摁
            var touchstartTime;//摁下时间
            overlay.on('touchstart', ' .gallerySlider img', function(e) {
                touchstartTime=+new Date();
                console.log("touchstartTime:"+touchstartTime)
                // var slider = parent.find(sliderClass);
                Changehua=true;
                var touch = e.originalEvent,
                    startX = touch.changedTouches[0].pageX;
                slider.on('touchmove', function(e) {
                    Changehua=false;
                    e.preventDefault();
                    touch = e.originalEvent.touches[0] ||
                        e.originalEvent.changedTouches[0];

                    if (touch.pageX - startX > 10) {
                        slider.off('touchmove');
                        showPrevious();
                    } else if (touch.pageX - startX < -10) {
                        slider.off('touchmove');
                        showNext();
                    }
                });


                // Return false to prevent image 
                // highlighting on Android
               // return false;

            }).on('touchend', function(e) {
               // console.log((e.target).className)
               var touchendTime=+new Date();
                 //长摁图片时间小于0.75s就是点击
                if((e.target).className!='ckyw' && (touchendTime- touchstartTime)/1000<0.75){
                    if(Changehua){
                        hideOverlay();
                        return false
                    }
                }


             /*   slider.off('touchmove');*/
            });

            // Listening for clicks on the thumbnail

            items.on('click', function(e) {
                e.preventDefault();


                // Find the position of this image
                // in the collection

                index = items.index($(this));
                showOverlay(index);
                showImage(index);

                // Preload the next image
                preload(index + 1);

                // Preload the previous
                preload(index - 1);

            });
            // If the browser does not have support 
            // for touch, display the arrows
            if (!("ontouchstart" in window)) {
                overlay.append(prevArrow).append(nextArrow);
                prevArrow.click(function(e) {
                    e.preventDefault();
                    showPrevious();
                });

                nextArrow.click(function(e) {
                    e.preventDefault();
                    showNext();
                });
            }
            // Listen for arrow keys
            $(window).bind('keydown', function(e) {

                if (e.keyCode == 37) {
                    showPrevious();
                } else if (e.keyCode == 39) {
                    showNext();
                }

            });

            /* Private functions */


            function showOverlay(index) {
                //var overlay = parent.find(overlayClass);
                // If the overlay is already shown, exit
                if (overlayVisible) {
                    return false;
                }

                // Show the overlay
                overlay.show();

                setTimeout(function() {
                    // Trigger the opacity CSS transition
                    overlay.addClass('visible');
                }, 100);

                // Move the slider to the correct image
                offsetSlider(index);

                // Raise the visible flag
                overlayVisible = true;
            }

            function hideOverlay() {
                var overlay = parent.find(overlayClass);
                // If the overlay is not shown, exit
                if (!overlayVisible) {
                    return false;
                }

                // Hide the overlay
                overlay.fadeOut(function(){
                    overlay.removeClass('visible');
                });
                overlayVisible = false;
            }

            function offsetSlider(index) {
                // This will trigger a smooth css transition
                slider.css('left', (-index * 100) + '%');
            }

            // Preload an image by its index in the items array
            function preload(index) {
                setTimeout(function() {
                    showImage(index);
                }, 1000);
            }

            // Show image in the slider
            function showImage(index) {

                // If the index is outside the bonds of the array
                if (index < 0 || index >= items.length) {
                    return false;
                }

                // Call the load function with the href attribute of the item
                //          loadImage(items.eq(index).attr('href'),function(){
                //              
                //              console.log($(this))
                //              placeholders.eq(index).html(this);
                //              placeholders.eq(index).append('<a href="#">dfasdffsadfsdfds</a>');
                //          });
                //console.log(this)

                /*loadImage2(items.eq(index).attr('href'),function(){           
                 placeholders.eq(index).append(this);
                 });
                 */
                loadImage2(items.eq(index).attr('href'), items.eq(index).attr('datayw'));

                function loadImage2(src, datayw) {
                    var img = $('<img>');
                    img.attr('src', src);
                    img.on('load', function() {
                        placeholders.eq(index).html(img);
                        if (datayw) {
                            placeholders.eq(index).append('<a href="' + datayw + '" class="ckyw">查看原文</a>');
                        }

                    });

                }
            }

            // Load the image and execute a callback function.
            // Returns a jQuery object

            //      function loadImage(src, callback){
            //          var img = $('<img>').on('load', function(){
            //              callback.call(img);     
            //          }); 
            //          img.attr('src',src);                    
            //      }
            /*修改*/





            function showNext() {

                // If this is not the last image
                if (index + 1 < items.length) {
                    index++;
                    offsetSlider(index);
                    preload(index + 1);
                } else {
                    // Trigger the spring animation

                    slider.addClass('rightSpring');
                    setTimeout(function() {
                        slider.removeClass('rightSpring');
                    }, 500);
                }
            }

            function showPrevious() {

                // If this is not the first image
                if (index > 0) {
                    index--;
                    offsetSlider(index);
                    preload(index - 1);
                } else {
                    // Trigger the spring animation

                    slider.addClass('leftSpring');
                    setTimeout(function() {
                        slider.removeClass('leftSpring');
                    }, 500);
                }
            }

        })















    };

})(jQuery);